package cn.kgc.vue.commons;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;

/**
 * @author 课工场
 * @date 2024/4/9
 * @description 统一的响应结果
 */
@Data
@ApiModel("ResponseResult")
public class ResponseResult {

    @ApiModelProperty(value = "状态码",example = "0")
    private Integer code;
    @ApiModelProperty(value  = "提示信息",example = "认证通过")
    private String message;
    @ApiModelProperty(value = "页面数据",example = "{username:'tom',age:20}")
    private HashMap<String,Object> data = new HashMap<>();

    private ResponseResult(){}
    // 成功
    public static ResponseResult success() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResponseStatus.SUCCESS.getCode());
        responseResult.setMessage(ResponseStatus.SUCCESS.getMessage());
        return responseResult;
    }

    // 错误
    public static ResponseResult fail() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResponseStatus.FAIL.getCode());
        responseResult.setMessage(ResponseStatus.FAIL.getMessage());
        return responseResult;
    }

    // 异常
    public static ResponseResult error() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResponseStatus.ERROR.getCode());
        responseResult.setMessage(ResponseStatus.ERROR.getMessage());
        return responseResult;
    }

    // 设置状态码
    public ResponseResult code(Integer code) {
        this.code = code;
        return this;
    }
    // 设置提示信息
    public ResponseResult message(String message) {
        this.message = message;
        return this;
    }
    // 设置数据  data
    public ResponseResult data(String key,Object data) {
        this.data.put(key,data);
        return this;
    }

}
